const { 
    register,
    login,
    isExist
} = require('../../controller/user');

const { genValidator } = require('../../middlewares/validator');
const userValidate = require('../../validator/user');

const router = require('koa-router')();

router.prefix('/api/user')

// 注册用户路由
router.post('/register', genValidator(userValidate), async ( ctx, next ) => {
    const { userName, password, gender } = ctx.request.body;
    ctx.body = await register({
        userName,
        password,
        gender
    })
})

// 登录路由
router.post('/login', async ( ctx, next ) => {
    const { userName, password } = ctx.request.body;
    ctx.body = await login( ctx, userName, password );
})

// 用户名是否存在
router.post('/isExist', async (ctx, next) => {
    const { userName } = ctx.request.body;
    ctx.body = await isExist(userName)
})

module.exports = router;